
package jpoco.internal.gson;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;

import java.util.Calendar;
import java.util.Date;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

class CustomCalendarTypeAdapter
    implements JsonDeserializer<java.util.Calendar> 

{

    private final DateFormat format;

    
    public CustomCalendarTypeAdapter(String datePattern) {
    	this.format = new SimpleDateFormat(datePattern);
    }
    


    public java.util.Calendar deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    	
    	
      if (!(json instanceof JsonPrimitive)) {
        throw new JsonParseException("The calendar should be a string value");
      }

      String dateString = json.getAsString();
      
      
      try 
      {
          Date d = format.parse(dateString);
    	  Calendar c = Calendar.getInstance();
          c.setTime(d);
          return c;
      } 
      catch (ParseException e) 
      {
    	  // todo : may need to rethink this
        return null;
      }
      
    }
    
}
